package com.aj.activity.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.aj.activity.NewChildActivity;
import com.aj.activity.R;
import com.aj.activity.dialog.ChooseChineseZodiacDialog;
import com.aj.activity.dialog.ChooseChineseZodiacDialog.IChineseZodiac;
import com.aj.activity.dialog.ChooseDateDialog;
import com.aj.activity.dialog.ChooseDateDialog.IChooseDay;
import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.User;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.ManagerChildRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.utils.PortraitLoaderUtil;

public class NewChildSmallFragment extends FamilyBaseFragment implements
		IChooseDay, IChineseZodiac {

	private static final String TAG = NewChildSmallFragment.class.getName();

	private ImageView header;

	private EditText enter_lovehomenumber;

	private EditText enter_name;

	private RadioGroup sex_radiogroup;

	private EditText enter_birth;

	private EditText enter_chinese_zodiac;

	private EditText enter_constellation;

	private Button sure_button;

	private String sex = "0";

	private String callId = "1";

	private String callName = "儿子";

	private String photoUrl;
	private String isPravite = "1";
	private UserDao userDao;
	private User user;

	private ChooseDateDialog chooseDateDialog;

	private ChooseChineseZodiacDialog chooseChineseZodiacDialog;

	private void showChooseDate() {
		if (chooseDateDialog == null) {
			chooseDateDialog = new ChooseDateDialog(getActivity());
			chooseDateDialog.setChooseDay(this);
		}
		chooseDateDialog.show();
	}

	private void showChooseChineseZodiacDialog(String kinds) {
		chooseChineseZodiacDialog = new ChooseChineseZodiacDialog(
				getActivity(), kinds);
		chooseChineseZodiacDialog.setChineseZodiac(this);
		chooseChineseZodiacDialog.show();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = LayoutInflater.from(getActivity()).inflate(
				R.layout.fragment_small_child, null);
		initView(view);
		return view;
	}

	private void initView(View view) {
		ButtonClick buttonClick = new ButtonClick();
		userDao = new UserDao(getActivity());
		user = userDao.getCurrentUser(LoveHomeApplication.token);
		header = (ImageView) view.findViewById(R.id.header);
		enter_lovehomenumber = (EditText) view
				.findViewById(R.id.enter_lovehomenumber);
		enter_name = (EditText) view.findViewById(R.id.enter_name);
		sex_radiogroup = (RadioGroup) view.findViewById(R.id.sex_radiogroup);
		sex_radiogroup
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						switch (checkedId) {
						case R.id.familyman:
							sex = "0";
							break;
						case R.id.familywomen:
							sex = "1";
							break;
						}
					}
				});
		enter_birth = (EditText) view.findViewById(R.id.enter_birth);
		enter_birth.setFocusable(false);
		enter_chinese_zodiac = (EditText) view
				.findViewById(R.id.enter_chinese_zodiac);
		enter_chinese_zodiac.setFocusable(false);

		enter_constellation = (EditText) view
				.findViewById(R.id.enter_constellation);
		enter_constellation.setFocusable(false);
		sure_button = (Button) view.findViewById(R.id.sure_button);

		header.setOnClickListener(buttonClick);
		enter_birth.setOnClickListener(buttonClick);
		enter_chinese_zodiac.setOnClickListener(buttonClick);
		enter_constellation.setOnClickListener(buttonClick);
		sure_button.setOnClickListener(buttonClick);

		callName = getActivity().getIntent().getStringExtra("callName");
		if (callName != null) {
			if (callName.equals("爸爸")) {
				callId = "1";
			} else if (callName.equals("妈妈")) {
				callId = "2";
			} else if (callName.equals("儿子")) {
				callId = "7";
			} else if (callName.equals("女儿")) {
				callId = "8";
			} else if (callName.equals("胎儿")) {
				callId = "13";
			}
		}
	}

	class ButtonClick implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.sure_button:
				if (enter_lovehomenumber.getText().toString().trim().equals("")) {
					Toast.makeText(getActivity(), "请输入爱称~", Toast.LENGTH_LONG)
							.show();
					return;
				}

				if (enter_name.getText().toString().trim().equals("")) {
					Toast.makeText(getActivity(), "请输入姓名~", Toast.LENGTH_LONG)
							.show();
				}
				if (enter_birth.getText().toString().trim().equals("")) {
					Toast.makeText(getActivity(), "请输入生日~", Toast.LENGTH_LONG)
							.show();
				}

				showProgressDialog();
				new ManagerChildRequest().excute(getActivity(), user
						.getUser_id(), "1", null, "0", photoUrl,
						enter_lovehomenumber.getText().toString(), enter_name
								.getText().toString(), sex, enter_birth
								.getText().toString(), null, isPravite, null,
						callId, null, new IHttpCallBack() {
							@Override
							public void success(String result) {
								try {
									dimissProgressDialog();
									Common common = new Common(new JSONObject(
											result));
									if (common
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
										getActivity().finish();
									}
									Toast.makeText(getActivity(),
											common.getErrorMsg(),
											Toast.LENGTH_LONG).show();
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}

							@Override
							public void fail(String result) {
								try {
									dimissProgressDialog();
									Common common = new Common(new JSONObject(
											result));
									Toast.makeText(getActivity(),
											common.getErrorMsg(),
											Toast.LENGTH_LONG).show();
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}
						});

				break;
			case R.id.enter_chinese_zodiac:
				showChooseChineseZodiacDialog("1");
				break;
			case R.id.enter_constellation:
				showChooseChineseZodiacDialog("2");
				break;
			case R.id.enter_birth:
				showChooseDate();
				break;
			case R.id.header:
				((NewChildActivity) getActivity()).openPanel();
				break;
			}
		}

	}

	public void setHeaderImage(String photoUrl, Bitmap bitmap) {
		header.setImageBitmap(bitmap);
		PortraitLoaderUtil.load(getActivity(), header, null, photoUrl,
				R.drawable.head_icon, R.drawable.head_icon);
		this.photoUrl = photoUrl;
	}

	@Override
	public void chooseDaySuccess(int year, int mouth, int day) {
		enter_birth.setText((year + "-" + mouth + "-" + day));
	}

	@Override
	public void chooseChineseZodiacSuccess(String chineseZodiac) {
		enter_chinese_zodiac.setText(chineseZodiac);
	}

	@Override
	public void chooseConstellationSuccess(String constellation) {
		enter_constellation.setText(constellation);
	}
}